package cn.tedu.store.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class LoginInterceptor implements HandlerInterceptor{

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		System.out.println("LoginInterceptor.preHandle");
		System.out.println("/trequest"+request);
		System.out.println("/tresponse"+response);
		System.out.println("/thandler"+handler);
		
		
		HttpSession session=request.getSession();
		if(session.getAttribute("uid")==null) {
			String url=request.getContextPath()+"/main/index.do";
			//没有登录，重定向到主页面
			response.sendRedirect(url);
			return false;
		}
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("LoginInterceptor.postHandle");
		System.out.println("/trequest"+request);
		System.out.println("/tresponse"+response);
		System.out.println("/thandler"+handler);
		System.out.println("/tmodelAndView"+modelAndView);
		
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("LoginInterceptor.afterCompletion");
		System.out.println("/trequest"+request);
		System.out.println("/tresponse"+response);
		System.out.println("/tex"+ex);
		
	}
	
}
